package com.taikang.hmp.hbusiness.diabetes.measure.medicine.action.intface;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.datastructre.support.ResponseDto;
import com.taikang.hmp.frame.common.web.action.BaseAction;
import com.taikang.hmp.hbusiness.diabetes.measure.medicine.service.IMedicineService;


/** 
 * 用药
 * author:qrp
 * time:2015年7月29日 下午2:32:42
 */
public class MedicineAction extends BaseAction{
	private static Logger logger = LoggerFactory.getLogger(MedicineAction.class);
	
	
	private IMedicineService medicineService;


	public IMedicineService getMedicineService() {
		return medicineService;
	}

	public void setMedicineService(IMedicineService medicineService) {
		this.medicineService = medicineService;
	}

	public String executeAction()  {
		if ("getMedicineList".equals(requestDto.getMethod())) {
			logger.info("--获取药品列表: getMedicineList method --");
			return getMedicineList();
		} else if ("saveMedicinePlan".equals(requestDto.getMethod())) {
			logger.info("--保存用药计划: saveMedicinePlan method --");
			return saveMedicinePlan();
		} else if ("getMedicinePlan".equals(requestDto.getMethod())) {
			logger.info("--获取用药计划: getMedicinePlan method--");
			return getMedicinePlan();
		} else if ("saveMedicineUse".equals(requestDto.getMethod())) {
			logger.info("--保存用药记录 : saveMedicineUse method --");
			return saveMedicineUse();
		} else {
			throw new TKException("no support method:"+requestDto.getMethod());
		}
	}

	/**
	 * 获取药品列表
	 * 
	 * @return
	 */
	public String getMedicineList() {
		ResponseDto responseDto = new ResponseDto();
		List<Dto> resultList = medicineService.getMedicineList();
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		responseDto.pushResultDto(resultDto);
		//
		super.setDataMap(responseDto);
		return SUCCESS;
	}

	/**
	 * 保存用药计划
	 */
	public String saveMedicinePlan() {	
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		//
		String state = paramDto.getAsString("state");
		if(state.equals("firstPlan")){
			medicineService.saveFirstMedicinePlan(paramDto);
		}else{
			medicineService.saveMedicinePlan(paramDto);
		}
		//
		super.setDataMap(responseDto);
		return SUCCESS;
	}

	/**
	 * 获取用药计划
	 */
	public String getMedicinePlan() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		//
		List<Dto> planList = medicineService.getMedicinePlan(paramDto);
		Dto resultDto = new BaseDto();
		resultDto.put("planlist", planList);
		//上次修改时间	
		resultDto.put("changedate", medicineService.getLastModifyDate(paramDto));	
		responseDto.pushResultDto(resultDto);
		//
		super.setDataMap(responseDto);
		return SUCCESS;
	}

	
	/**
	 * 保存用药记录
	 */
	public String saveMedicineUse() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
	    Dto paramDto = requestDto.getRequestlist().get(0);
	    paramDto = super.appendRequestBase(paramDto, requestDto);
	    //
	    medicineService.saveMedicineUse(paramDto);
	    //
		super.setDataMap(responseDto);
		return SUCCESS;
	}

}
